
//
//  RDSettingView.m
//  eCourseApp
//
//  Created by 飞猪 on 2018/9/17.
//  Copyright © 2018年 Facebook. All rights reserved.
//

#import "RDSettingView.h"

@implementation RDButton

- (void)setSelected:(BOOL)selected {
  [super setSelected:selected];
  
  if (selected) {
    self.layer.borderColor = UIColorHex(A35046).CGColor;
    self.layer.borderWidth = 2.f;
  }
  else {
    self.layer.borderWidth = 0.f;
  }
}

@end

@interface RDSettingView ()

@end

@implementation RDSettingView

- (void)awakeFromNib {
  [super awakeFromNib];
  
  // 设置thumb normal时的图片
  UIImage *thumbImageNormal = [UIImage imageNamed:@"slider_Dot"];
  
  [self.slider setThumbImage:thumbImageNormal forState:UIControlStateNormal];
  
  self.slider.minimumTrackTintColor = UIColorHex(A35046);
  
  // 添加背景颜色
  for (NSInteger i = 0; i < 6; ++i) {
    RDButton *btn = [RDButton buttonWithType:UIButtonTypeCustom];
    btn.tag = i;
    [btn setBackgroundColor:self.hexColors[i]];
    btn.layer.cornerRadius = 14.f;
    btn.layer.masksToBounds = YES;
    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttons setObject:btn forKey:@(i)];
    [self addSubview:btn];
  }
}

- (void)layoutSubviews {
  [super layoutSubviews];
  
  [self.buttons.allValues mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:32 leadSpacing:22 tailSpacing:22];
  [self.buttons.allValues mas_makeConstraints:^(MASConstraintMaker *make) {
    make.height.mas_equalTo(28);
    make.centerY.mas_equalTo(self);
  }];
}

- (void)btnClicked:(RDButton *)btn {
  
  self.bgColor.selected = NO;
  btn.selected = YES;
  self.bgColor = btn;
  
  if (self.delegate && [self.delegate respondsToSelector:@selector(settingView:clickedIndex:value:)]) {
    [self.delegate settingView:self clickedIndex:RDSettingClickTypeColor value:[self.colors objectAtIndex:btn.tag]];
  }
}

- (IBAction)fontSettingClicked:(UIButton *)sender {
  
  self.sender.selected = NO;
  sender.selected = YES;
  self.sender = sender;
  
  if (self.delegate && [self.delegate respondsToSelector:@selector(settingView:clickedIndex:value:)]) {
    [self.delegate settingView:self clickedIndex:RDSettingClickTypeFont value:@(sender.tag)];
  }
  
}

- (IBAction)sliderChanged:(UISlider *)sender {
  
  [[UIScreen mainScreen] setBrightness:sender.value];
  
  [[NSUserDefaults standardUserDefaults] setObject:@(sender.value) forKey:@"Brightness"];
  [[NSUserDefaults standardUserDefaults] synchronize];
  
}

- (NSMutableDictionary *)buttons {
  if (!_buttons) {
    _buttons = [NSMutableDictionary dictionary];
  }
  return _buttons;
}

- (NSArray *)colors {
  if (!_colors) {
    _colors = @[
                @"B4B2B5",
                @"ADA79E",
                @"A39470",
                @"879F84",
                @"2F415E",
                @"50423E"
                ];
  }
  return _colors;
}

- (NSArray *)hexColors {
  if (!_hexColors) {
    _hexColors = @[
                UIColorHex(B4B2B5),
                UIColorHex(ADA79E),
                UIColorHex(A39470),
                UIColorHex(879F84),
                UIColorHex(2F415E),
                UIColorHex(50423E)
                ];
  }
  return _hexColors;
}


@end
